/*
 * Copyright 2022-2024 兮玥(190785909@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chestnut.common.config;

import static com.google.code.kaptcha.Constants.KAPTCHA_BORDER;
import static com.google.code.kaptcha.Constants.KAPTCHA_BORDER_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_IMAGE_HEIGHT;
import static com.google.code.kaptcha.Constants.KAPTCHA_IMAGE_WIDTH;
import static com.google.code.kaptcha.Constants.KAPTCHA_NOISE_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_NOISE_IMPL;
import static com.google.code.kaptcha.Constants.KAPTCHA_OBSCURIFICATOR_IMPL;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_CONFIG_KEY;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_IMPL;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/**
 * 验证码配置
 * 验证码类型 math 数组计算 char 字符验证
 *
 * @author 兮玥
 * @email 190785909@qq.com
 */
@Configuration
public class CaptchaConfig {
	
	private static final String KaptchaTextCreator = "com.chestnut.common.captcha.KaptchaTextCreator";
	
	public static final String BEAN_PREFIX = "Kaptcha_";
	
	@Bean(BEAN_PREFIX + "char")
	public DefaultKaptcha getKaptchaBean() {
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		// 是否有边框 默认为true 我们可以自己设置yes，no
		properties.setProperty(KAPTCHA_BORDER, "yes");
		// 验证码文本字符颜色 默认为Color.BLACK
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
		// 验证码图片宽度 默认为200
		properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
		// 验证码图片高度 默认为50
		properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
		// 验证码文本字符大小 默认为40
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
		// KAPTCHA_SESSION_KEY
		properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
		// 验证码文本字符长度 默认为5
		properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
		// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
		// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple
		// 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
		// 阴影com.google.code.kaptcha.impl.ShadowGimpy
		properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}

	@Bean(BEAN_PREFIX + "math")
	public DefaultKaptcha getKaptchaBeanMath() {
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		// 是否有边框 默认为true 我们可以自己设置yes，no
		properties.setProperty(KAPTCHA_BORDER, "yes");
		// 边框颜色 默认为Color.BLACK
		properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
		// 验证码文本字符颜色 默认为Color.BLACK
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
		// 验证码图片宽度 默认为200
		properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
		// 验证码图片高度 默认为50
		properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
		// 验证码文本字符大小 默认为40
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
		// KAPTCHA_SESSION_KEY
		properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
		// 验证码文本生成器
		properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, KaptchaTextCreator);
		// 验证码文本字符间距 默认为2
		properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
		// 验证码文本字符长度 默认为5
		properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
		// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
		properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
		// 验证码噪点颜色 默认为Color.BLACK
		properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
		// 干扰实现类
		properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
		// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple
		// 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
		// 阴影com.google.code.kaptcha.impl.ShadowGimpy
		properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}
}
